﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace TorneoLoL
{
    public partial class FormBorrar : Form
    {

        private int eleccion;

        public FormBorrar(int aux) : this()
        {
            this.eleccion = aux;
        }

        Conexion conexion = new Conexion();
        DataSet ds = new DataSet();
        String ConsultaSQL;

        public FormBorrar()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

        }

        private void FormBorrar_Load(object sender, EventArgs e)
        {

            Size size = new Size();

            switch (eleccion)
            {
                case 1:

                    size.Height = 446;
                    size.Width = 843;
                    grd_Main.Size = size;

                    label1.Text = "ID Team:";
                    label1.Location = new Point(925, 12);

                    tb_ID.Location = new Point(1001, 12);

                    btnBorrar.Location = new Point(1043, 12);

                    ConsultaSQL = "select Teams.T_NombreTeam as [Nombre Team], " +
                        "Teams.T_NombreCompleto as [Nombre Completo], Paises.P_Nombre as [País], " +
                        "Coaches.C_Nombre as [Nombre Coach], Coaches.C_Nick as [Nick Coach] " +
                        "from Teams inner join Coaches on T_idCoach = C_Id inner join Paises on T_Pais = P_ID";

                    conexion.consultarDataSet(ref ds, "Teams", ConsultaSQL);
                    grd_Main.DataSource = ds.Tables["Teams"];

                    break;

                case 2:

                    size.Height = 446;
                    size.Width = 1200;
                    grd_Main.Size = size;

                    label1.Text = "ID Jugador:";
                    label1.Location = new Point(12, 476);

                    tb_ID.Location = new Point(107, 476);

                    btnBorrar.Location = new Point(149, 476);

                    ConsultaSQL = "select Jugadores.J_Nombre as[Nombre], Jugadores.J_Nick as[Nick], " +
                    "Jugadores.J_Rol as[Rol], Teams.T_NombreTeam as[Nombre Team], " +
                    "Jugadores.J_FechaN as[Fecha Nacimiento], Paises.P_Nombre as[País] " +
                    "from Jugadores inner join Teams on Jugadores.J_IdTeam = Teams.T_IdTeam " +
                    "inner join Paises on Jugadores.J_Pais = Paises.P_ID";

                    conexion.consultarDataSet(ref ds, "Jugadores", ConsultaSQL);
                    grd_Main.DataSource = ds.Tables["Jugadores"];

                    break;

                case 3:

                    size.Height = 446;
                    size.Width = 843;
                    grd_Main.Size = size;

                    label1.Text = "ID Coach:";
                    label1.Location = new Point(925, 12);

                    tb_ID.Location = new Point(1001, 12);

                    btnBorrar.Location = new Point(1043, 12);

                    ConsultaSQL = "select Coaches.C_Nick as[Nick], Coaches.C_Nombre as[Nombre], " +
                        "Coaches.C_FechaN as[Fecha Nacimiento], Paises.P_Nombre from Coaches " +
                        "inner join Paises on C_Pais = P_ID";

                    conexion.consultarDataSet(ref ds, "Coaches", ConsultaSQL);
                    grd_Main.DataSource = ds.Tables["Coaches"];

                    break;

            }

        }

        private void grd_Main_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = grd_Main.CurrentRow.Index;

            tb_ID.Text = grd_Main[0, row].Value.ToString();
            if (tb_ID.Text != string.Empty)
            {
                btnBorrar.Enabled = true;
            }
            else
            {
                btnBorrar.Enabled = false;

            }


        }

        private void btnBorrar_Click(object sender, EventArgs e)
        {
            switch (eleccion)
            {
                case 1:

                    ConsultaSQL = "select J_IdTeam from Jugadores where J_IdTeam = " + tb_ID.Text;

                    conexion.consultarDataSet(ref ds, "Jugadores_consulta", ConsultaSQL);


                    if (ds.Tables["Jugadores_consulta"].Rows.Count != 0)
                    {
                        ds.Tables["Jugadores_consulta"].Clear();
                        MessageBox.Show("Error. El team tiene jugadores cargados.");
                    }
                    else
                    {
                        ds.Tables["Jugadores_consulta"].Clear();
                        ConsultaSQL = "delete Teams where T_IdTeam = " + tb_ID.Text;
                        conexion.consultarDataSet(ref ds, "Teams", ConsultaSQL);
                        grd_Main.DataSource = ds.Tables["Teams"];

                    }

                    break;

                case 2:

                    ConsultaSQL = "select DJ_IdJugador from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + tb_ID.Text;

                    conexion.consultarDataSet(ref ds, "DesempeñoDeJugadorXEncuentro_consulta", ConsultaSQL);

                    if (ds.Tables["DesempeñoDeJugadorXEncuentro_consulta"].Rows.Count != 0)
                    {
                        ds.Tables["DesempeñoDeJugadorXEncuentro_consulta"].Clear();
                        MessageBox.Show( "Error. El jugador tiene partidas cargadas.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                    }
                    else
                    {
                        ds.Tables["DesempeñoDeJugadorXEncuentro_consulta"].Clear();
                        ConsultaSQL = "delete Jugadores where J_Id = " + tb_ID.Text;
                        conexion.consultarDataSet(ref ds, "Jugadores", ConsultaSQL);
                        grd_Main.DataSource = ds.Tables["Jugadores"];
                        
                    }


                    break;

                case 3:

                    ConsultaSQL = "select T_idCoach from Teams where T_idCoach = " + tb_ID.Text;

                    conexion.consultarDataSet(ref ds, "Teams_consulta", ConsultaSQL);


                    
                    if (ds.Tables["Teams_consulta"].Rows.Count != 0)
                    {
                        ds.Tables["Teams_consulta"].Clear();
                        MessageBox.Show( "Error. El coach tiene teams cargados.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                    }
                    else
                    {
                        ds.Tables["Teams_consulta"].Clear();
                        ConsultaSQL = "delete Coaches where C_Id = " + tb_ID.Text;
                        conexion.consultarDataSet(ref ds, "Coaches", ConsultaSQL);
                        grd_Main.DataSource = ds.Tables["Coaches"];

                    }
                    
                    break;

            }

        }

    }
}
            
